﻿namespace AgileDashboard.controls
{
    using System;
    using System.Collections.Specialized;
    using System.Web.UI;

    using AgileDashboard.AppCode;

    public partial class ReleaseGraph : UserControl
    {
        public int GraphWidth = 500; // default
        public string Release;
        public string Color = "#AA0000"; // default
        public string FadeToColor;

        protected double TotalStoryPoints;
        protected double DoneStoryPoints;

        protected int ScaledDone;
        public string IterationSchedule;


        public ReleaseGraph()
        {
        }

        public ReleaseGraph(NameValueCollection collection)
        {
            this.IterationSchedule = collection["IterationSchedule"];
            this.Release = collection["Release"];
            this.GraphWidth = int.Parse(collection["GraphWidth"]);
        }


        #region Overrides of Control

        protected override void Render(HtmlTextWriter writer)
        {
            this.Color = Util.FuzzyColorPicker(this.Release);

            // If a 'fade to' color is not specified, use a default value
            if (string.IsNullOrEmpty(this.FadeToColor))
            {
                this.FadeToColor = Util.ScaleColor(this.Color, .25);
            }

            var dao = new ProjectDao(new RestDataConnection());
            var proj = dao.GetProjectByName(this.Release);

            this.DoneStoryPoints = proj.DoneStoryPoints;

            this.TotalStoryPoints = proj.TotalStoryPoints;

            if (this.TotalStoryPoints > 0)
            {
                var scaleFactor = this.DoneStoryPoints / this.TotalStoryPoints;

                this.ScaledDone = Convert.ToInt32(Math.Round(this.GraphWidth * scaleFactor));

                const int GraphPadding = 12;

                // if the graph is too wide...
                if (this.ScaledDone > (this.GraphWidth - GraphPadding))
                {
                    // leave some padding at the end
                    this.ScaledDone = this.GraphWidth - GraphPadding;
                }
            }

            base.Render(writer);
        }

        #endregion
    }
}